package com.redijedi.tapestry.components;

import java.util.Calendar;
import java.util.Date;

import org.apache.tapestry.SelectModel;
import org.apache.tapestry.ValueEncoder;
import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.annotations.SetupRender;
import org.apache.tapestry.corelib.base.AbstractField;
import org.apache.tapestry.corelib.components.Select;
import org.apache.tapestry.services.FormSupport;
import org.apache.tapestry.services.Request;

import com.redijedi.tapestry.internal.datefield.DateUtils;
import com.redijedi.tapestry.util.SelectModelImpl;
import com.redijedi.tapestry.util.StringValueEncoder;

/**
 * @author torr
 * 
 */
public class DateField extends AbstractField {

	@Inject
	private Request _request;

	/**
	 * The value of this component.
	 */
	@Parameter(required = true)
	private Date _value;

	@SuppressWarnings("unused")
	@Parameter(value = "false")
	private boolean _disabled;

	@SuppressWarnings("unused")
	@Component
	private Element _element;

	@SuppressWarnings("unused")
	@Component(parameters = { "value=monthValue", "model=monthsSelectModel",
			"encoder=encoder", "disabled=inherit:disabled" })
	private Select _monthSelect;

	@SuppressWarnings("unused")
	@Component(parameters = { "value=dayValue", "model=daysSelectModel",
			"encoder=encoder", "disabled=inherit:disabled" })
	private Select _daySelect;

	@SuppressWarnings("unused")
	@Component(parameters = { "value=yearValue", "model=yearsSelectModel",
			"encoder=encoder", "disabled=inherit:disabled" })
	private Select _yearSelect;

	private Integer _fromYear;

	private Integer _toYear;

	private String _monthValue;

	private String _dayValue;

	private String _yearValue;

	private ValueEncoder<String> _encoder = new StringValueEncoder();

	private Calendar _calendar = Calendar.getInstance();

	public ValueEncoder<String> getEncoder() {
		return _encoder;
	}

	public String getMonthValue() {
		return _monthValue;
	}

	public void setMonthValue(String value) {
		_monthValue = value;
	}

	public String getDayValue() {
		return _dayValue;
	}

	public void setDayValue(String value) {
		_dayValue = value;
	}

	public String getYearValue() {
		return _yearValue;
	}

	public void setYearValue(String value) {
		_yearValue = value;
	}

	public SelectModel getMonthsSelectModel() {
		return new SelectModelImpl(DateUtils.getMonthOptionModels());
	}

	public SelectModel getDaysSelectModel() {
		return new SelectModelImpl(DateUtils.getDayOptionModels());
	}

	public SelectModel getYearsSelectModel() {
		return new SelectModelImpl(DateUtils.getYearOptionModels(_fromYear,
				_toYear));
	}

	@SetupRender
	void setupComponent() {
		Date date = _value;
		if (date != null) {
			_calendar.setTime(date);
		}

		_fromYear = _calendar.get(Calendar.YEAR) - 2;
		_toYear = _calendar.get(Calendar.YEAR) + 3;

		_monthValue = String.valueOf(_calendar.get(Calendar.MONTH));
		_dayValue = String.valueOf(_calendar.get(Calendar.DATE));
		_yearValue = String.valueOf(_calendar.get(Calendar.YEAR));
	}

	@Override
	protected void processSubmission(FormSupport arg0, String elementName) {
		String month = _request.getParameter("monthSelect");
		String day = _request.getParameter("daySelect");
		String year = _request.getParameter("yearSelect");

		int intYear = Integer.parseInt(year);
		int intMonth = Integer.parseInt(month);
		int intDay = Integer.parseInt(day);

		_calendar.set(intYear, intMonth, intDay);

		_value = _calendar.getTime();
	}

}
